home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1220 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: newsserver.trl.OZ.AU!usenet
  2. From: t.hand@trl.oz.au (Trevor Hand)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Need help on "string" conditional statements
  5. Date: 9 Jan 1996 23:02:55 GMT
  6. Organization: Telstra Research Laboratories
  7. Message-ID: <4cus6v$383@newsserver.trl.OZ.AU>
  8. References: <4beid0$h0e@news.ios.com> <30EF60C5.58CB@www.luminaire.com>
  9. NNTP-Posting-Host: tmp_ip_159.trl.oz.au
  10. X-Newsreader: News Xpress Version 1.0 Beta #3
  11.  
  12. In article <30EF60C5.58CB@www.luminaire.com>,
  13.    "Edwin P. Jacques" <pierre@www.luminaire.com> wrote:
  14. >Keith Boruff wrote:
  15. >> 
  16. >> I am trying to enter a string in a program and check this string with a
  17. >> conditional "if" statement:
  18. >> 
  19. >> char StringName[3];
  20. >> 
  21. >> cout << "Enter string";
  22. >> cin >> StringName;  // I enter "Jan" without quote marks
  23. >> 
  24. >> if (StringName == "Jan")
  25. >
  26. If you want to compare StringName to an array of characters try using strcmp 
  27. eg.  if (strcmp(StringName,"Jan"))  If the two strings are equal ie. the 
  28. same, strcmp will return a 0, which is of course "false".  To get over the 
  29. problem and make the result appear to the programmer to be what is expected I 
  30. would change the statement to -
  31.  
  32. if (!(strcmp(StringName,"Jan")))
  33.   //Statements if the same
  34. else
  35.   //Statements if different
  36.  
  37. If you want to make a case insensitive comparison use stricmp in place of 
  38. strcmp.   I had the same problems at first too, I guess it's my BASIC 
  39. background!!
  40.  
  41. Usually I use the string class though, it makes life a LOT easier.
  42.  
  43. Trevor Hand
  44. t.hand@trl.oz.au
  45.